//
//  ZJQADetailViewController.m
//  ShangHaiHuDongApp
//
//  Created by ZJ on 2017/6/22.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "ZJQADetailViewController.h"
#import "ZJQADetailHeaderView.h"
#import "ZJQADetailTableViewCell.h"
#import "ZJRefurbish.h"
#define CELLID @"ZJQADetailTableViewCell"
@interface ZJQADetailViewController ()<UITableViewDelegate,UITableViewDataSource>

/** headerView */
@property (nonatomic, strong) ZJQADetailHeaderView *headerView;
/** tableView */
@property (nonatomic, strong) UITableView *tableView;
/** 数据 */
@property (nonatomic, strong) NSMutableArray *dataArray;
/** 刷新 */
@property (nonatomic, strong) ZJRefurbish *refurbish;
/**  分页  */
@property (assign, nonatomic) NSInteger  page;
@end

@implementation ZJQADetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.titlelabel.text = @"问题详情";
    [self.view addSubview:self.tableView];
    
    __weak typeof(self) weakSelf = self;
    [self.refurbish headerToScollview:self.tableView Block:^{
        weakSelf.page = 1;
        [weakSelf netWorking_header];
    }];
    
    [self.refurbish footerToScollview:self.tableView Block:^{
        weakSelf.page ++;
        [weakSelf netWorking_footer];
    }];;
    [self.tableView.mj_header beginRefreshing];
}

- (void)netWorking_header{
    NSDictionary *dict = @{@"QUESTION_ID":self.model.ID,
                           @"PAGESTART":@"1",
                           @"PAGECOUNT":@"15"};
    __weak typeof(self) weakSelf = self;
    [NetWorkingManager postWithUrlString:[NetWorkingURL qaactionAnswer] Parameter:dict Success:^(id object) {
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
        if ([model.STATE isEqualToString:@"20000"]) {
            weakSelf.dataArray = [[NSArray yy_modelArrayWithClass:[ZJQADetailCellModel class] json:[object objectForKey:@"ANSWER_MEG"]] mutableCopy];
            [weakSelf.tableView reloadData];
        }
        [weakSelf.tableView.mj_header endRefreshing];
    } Fail:^(id object) {
        [weakSelf.tableView.mj_header endRefreshing];
    }];
}
- (void)netWorking_footer{
    NSDictionary *dict = @{@"QUESTION_ID":self.model.ID,
                           @"PAGESTART":[NSString stringWithFormat:@"%ld",_page],
                           @"PAGECOUNT":@"15",
                           };
    __weak typeof(self) weakSelf = self;
    [NetWorkingManager postWithUrlString:[NetWorkingURL qaactionAnswer] Parameter:dict Success:^(id object) {
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
        if ([model.STATE isEqualToString:@"20000"]) {
            NSArray * array = [NSArray yy_modelArrayWithClass:[ZJQADetailCellModel class] json:[object objectForKey:@"ANSWER_MEG"]];
            [weakSelf.dataArray addObjectsFromArray:array];
            [weakSelf.tableView reloadData];
        }
        [weakSelf.tableView.mj_footer endRefreshing];
    } Fail:^(id object) {
        [weakSelf.tableView.mj_footer endRefreshing];
    }];
}




#pragma mark - UITableView 协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ZJQADetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CELLID];
    cell.model = self.dataArray[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    ZJQADetailCellModel *model = self.dataArray[indexPath.row];
    
    return model.cellHeight;
}

#pragma mark - Setter


#pragma mark - Getters
- (ZJQADetailHeaderView *)headerView{
    if (_headerView == nil){
        _headerView = [[ZJQADetailHeaderView alloc]initWithFrame:CGRectMake(0, LP_TopHeight, self.view.width, self.model.cellHeight)];
        _headerView.model = self.model;
    }
    return _headerView;
}

- (UITableView *)tableView{
    if (_tableView == nil){
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, LP_TopHeight, self.view.width, self.view.height - LP_TopHeight) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableHeaderView = self.headerView;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerClass:[ZJQADetailTableViewCell class] forCellReuseIdentifier:CELLID];
    }
    return _tableView;
}
- (NSMutableArray *)dataArray{
    if (_dataArray == nil){
        _dataArray = [NSMutableArray array];
        
    }
    return _dataArray;
}
- (ZJRefurbish *)refurbish{
    if (_refurbish == nil){
        _refurbish = [ZJRefurbish new];
        
    }
    return _refurbish;
}
@end
